package one.stand.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.feign.WechatAdminFegin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import one.stand.mapper.CompanyAccountMapperExt;
import one.stand.model.CompanyAccountModel;
import one.stand.model.ResultModel;
import one.stand.request.CompanyAccountRequest;
import one.stand.request.VersionRequest;
import one.stand.response.CompanyAccountResponse;
import one.stand.response.VersionResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: ZRH
 * @CreateDate: 2019/4/17 9:45
 */
@RestController
@RequestMapping("adminApi/Wechat")
@Api(description = "总后台-小程序管理")
@Slf4j
public class WechatController extends BaseController {
    @Autowired
    protected WechatAdminFegin wechatAdminFegin;

    @Autowired
    private CompanyAccountMapperExt companyAccountMapperExt;

    @ApiOperation(value = "本地小程序版本代码管理列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "versionType", value = "1,用户端，2crm端", paramType = "Integer"),
    })
    @PostMapping("appletCodeManageList")
    @SecurityAuthCheck
    public ResultModel<Map<String, Object>> appletCodeManageList(VersionRequest request) {
        return adminFegin.appletCodeManageList(request);
    }

    @ApiOperation(value = "本地小程序版本代码管理详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "versionId", value = "小程序代码管理表id", required = true, paramType = "Integer"),
    })
    @PostMapping("appletCodeManageInfo")
    @SecurityAuthCheck
    public ResultModel<VersionResponse> appletCodeManageInfo(VersionRequest request) {
        return adminFegin.appletCodeManageInfo(request);
    }

    @ApiOperation(value = "添加小程序版本代码信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "versionNum", value = "版本号", required = true, paramType = "String"),
            @ApiImplicitParam(name = "appId", value = "appId", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "templateId", value = "模板id", required = true, paramType = "String"),
            @ApiImplicitParam(name = "desc", value = "描述", required = true, paramType = "String"),
    })
    @PostMapping("addVersionInfo")
    @SecurityAuthCheck
    public ResultModel<Boolean> addVersionInfo(VersionRequest request) {
        return adminFegin.addVersionInfo(request);
    }

    @ApiOperation(value = "查询草稿箱列表")
    @PostMapping("getTemplateDraftList")
    @SecurityAuthCheck
    public ResultModel<JSONObject> getTemplateDraftList() {
        return ResultModel.success(JSON.parseObject(wechatAdminFegin.getTemplateDraftList()));
    }

    @ApiOperation(value = "查询小程序模板库列表")
    @PostMapping("getTemplateList")
    @SecurityAuthCheck
    public ResultModel<JSONObject> getTemplateList() {
        return ResultModel.success(JSON.parseObject(wechatAdminFegin.getTemplateList()));
    }

    @ApiOperation(value = "把草稿存为模板接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "draftId", value = "draftId", required = true, paramType = "String"),
    })
    @PostMapping("addTotemplate")
    @SecurityAuthCheck
    public ResultModel<String> addTotemplate(String draftId) {
        return ResultModel.success(wechatAdminFegin.addTotemplate(draftId));
    }

    @ApiOperation(value = "代码提交到相应小程序")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "appId",  paramType = "String"),
            @ApiImplicitParam(name = "versionId", value = "versionId", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "submitAudit", value = "true:立即提交审核", required = true, paramType = "boolean"),
    })
    @PostMapping("commit")
    @SecurityAuthCheck
    public ResultModel commit(String appId, Integer versionId,boolean submitAudit) {
        if(StringUtils.isBlank(appId)){
            ResultModel<List<String>> resultModel = adminFegin.selectAppId(versionId);
            if(resultModel.isSuccess() && resultModel.getData() != null) {
                for (String str : resultModel.getData()) {
                    wechatAdminFegin.commit(str, versionId,submitAudit);
                }
                return ResultModel.success();
            }else{
                return resultModel;
            }
        }
        return ResultModel.success(wechatAdminFegin.commit(appId, versionId,submitAudit));
    }


    @ApiOperation(value = "小程序审核撤回\n")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "appId", required = true, paramType = "String"),
    })
    @PostMapping("undoCodeAudit")
    @SecurityAuthCheck
    public ResultModel undoCodeAudit(String appId) {
        return ResultModel.success(wechatAdminFegin.undoCodeAudit(appId));
    }

    @ApiOperation(value = "发布小程序")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "versionId", value = "小程序版本代码id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "appId", value = "appId", required = true, paramType = "String"),
    })
    @PostMapping("release")
    @SecurityAuthCheck
    public ResultModel release(Integer versionId,String appId) {
        return ResultModel.success(wechatAdminFegin.release(versionId,appId));
    }

    @ApiOperation(value = "查询公司账号列表")
    @PostMapping("selectCompanyAccountList")
    public ResultModel<Map<String, Object>> selectCompanyAccountList(CompanyAccountRequest request) {
        log.info("查询公司账号列表：{}", JSON.toJSONString(request));
        CompanyAccountModel model = new CompanyAccountModel();
        if (request != null) {
            BeanUtils.copyProperties(request, model);
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<CompanyAccountResponse> list = companyAccountMapperExt.selectCompanyAccountList(model);
        if (list == null || list.size() <= 0) {
            return ResultModel.noData();
        }
        PageInfo<CompanyAccountResponse> pageInfo = new PageInfo(list);
        Map<String, Object> map = new HashMap<>(1);
        map.put("pageInfo", pageInfo);
        return ResultModel.success(map);
    }

    @ApiOperation(value = "获取体验码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "appId", required = true, paramType = "String"),
    })
    @PostMapping("getQrCode")
    @SecurityAuthCheck
    public String getQrCode(String appId) {
        return wechatAdminFegin.getQrCode(appId);
    }

    /**
     * 获取最新审核结果
     *
     * @return
     */
    @ApiOperation(value = "获取审核结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "versionId", value = "版本id", required = true, paramType = "String"),
            @ApiImplicitParam(name = "appId", value = "appId", required = true, paramType = "String"),
            @ApiImplicitParam(name = "release", value = "true审核通过立即发布", required = true, paramType = "boolean"),
    })
    @PostMapping("getLatestAuditStatus")
    @SecurityAuthCheck
    public ResultModel<String> getLatestAuditStatus(Integer versionId,String appId,boolean release) {
        return ResultModel.success(wechatAdminFegin.getLatestAuditStatus(versionId,appId,release));
    }


    /**
     * 提交审核接口
     * @param versionId
     * @return
     */
    @ApiOperation(value = "提交审核接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "versionId", value = "版本id", required = true, paramType = "String"),
            @ApiImplicitParam(name = "appId", value = "appId", required = true, paramType = "String"),
    })
    @PostMapping("submitAudit")
    @SecurityAuthCheck
    public ResultModel<String> submitAudit(Integer versionId,String appId) {
        return ResultModel.success(wechatAdminFegin.submitAudit(versionId,appId));
    }
}
